package com.guigu.controller;

import com.guigu.dto.roleDTO;
import com.guigu.dto.rolemenuDTO;
import com.guigu.entity.meals;
import com.guigu.entity.role;
import com.guigu.exception.SsmException;
import com.guigu.service.roleService;
import com.guigu.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class roleController {
    @Autowired
    private roleService roleservice;

    @PostMapping("/query")
    public Result query(@RequestBody roleDTO dto) {
        try {
            List<roleVO> list = this.roleservice.query(dto);
            System.err.println(dto);
            if (!list.isEmpty()) {
                return new Result<List<roleVO>>("200", "true", "查询成功", list);
            } else if (dto.getName() != null) {
                return new Result<List<roleVO>>("200", "true", "没有对应的数据", list);
            } else {
                return new Result<List<roleVO>>("400", "false", "查询失败", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new SsmException("异常");
        }
    }

    @PostMapping("/add")
    public Result add(@RequestBody role ro) {
        try {

            int i = this.roleservice.add(ro);
            if (i == 1) {
                int id = this.roleservice.maxid();
                this.roleservice.addacrole(id);
                return new Result<>("200", "true", "添加成功", null);

            } else {
                return new Result<>("400", "false", "添加失败", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new SsmException("异常");
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody role ro) {
        try {
            int id = this.roleservice.update(ro);
            if (id == 1) {
                return new Result<>("200", "true", "修改成功", null);
            } else {
                return new Result<>("400", "false", "修改失败", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new SsmException("异常");
        }
    }

    @RequestMapping("/querymenu")
    public Map<cdVO, List<cdVO>> queryMenu(@RequestBody rolemenuDTO ro) {
        //查询到账户具有菜单（父菜单和子菜单）
        List<cdVO> menulist = roleservice.querymenu(ro);
        //父菜单对象为建，子菜单集合为值
        Map<cdVO, List<cdVO>> menumap = new HashMap<cdVO, List<cdVO>>();
        for (int i = 0; i < menulist.size(); i++) {
            cdVO menu = menulist.get(i);
            //判断副菜单id为0的话就是副菜单
            if (menu.getParentid() == 0) {
                List<cdVO> childmenus = new ArrayList<cdVO>();
                //当前menu对象是父菜单
                for (int j = 0; j < menulist.size(); j++) {
                    cdVO temp = menulist.get(j);
                    if (temp.getParentid() == menu.getId()) {
                        //当前temp菜单对象和父菜单编号和menu菜单对象编号相同temp是menu的子菜单
                        childmenus.add(temp);
                    }
                }
                menumap.put(menu, childmenus);
            }
        }
        System.err.println(menumap);
        return menumap;
    }

    @RequestMapping("/queryzmenu")
    public Map<cdVO, List<cdVO>> queryzMenu(@RequestBody rolemenuDTO ro) {
        //查询到账户具有菜单（父菜单和子菜单）
        List<cdVO> menulist = roleservice.queryzmenu(ro);
        //父菜单对象为建，子菜单集合为值
        Map<cdVO, List<cdVO>> menumap = new HashMap<cdVO, List<cdVO>>();
        for (int i = 0; i < menulist.size(); i++) {
            cdVO menu = menulist.get(i);
            //判断副菜单id为0的话就是副菜单
            if (menu.getParentid() == 0) {
                List<cdVO> childmenus = new ArrayList<cdVO>();
                //当前menu对象是父菜单
                for (int j = 0; j < menulist.size(); j++) {
                    cdVO temp = menulist.get(j);
                    if (temp.getParentid() == menu.getId()) {
                        //当前temp菜单对象和父菜单编号和menu菜单对象编号相同temp是menu的子菜单
                        childmenus.add(temp);
                    }
                }
                menumap.put(menu, childmenus);
            }
        }
        System.err.println(menumap);
        return menumap;
    }

    @PostMapping("/adddelmenu")
    public Result adddelmenu(@RequestBody rolemenuDTO ro) {
        System.err.println(ro);
        try{
            int i = 0;
            int a=0;
            if(!ro.getAdd().isEmpty()){
                i=this.roleservice.insertrolemenu(ro);
            }
            if(!ro.getDel().isEmpty()){
                a=this.roleservice.deleterolemenu(ro);
            }
            if(i+a!=0){
                return new Result<>("200", "true", "修改成功", null);
            }else if(ro.getAdd().isEmpty()&&ro.getDel().isEmpty()){
                return new Result<>("200", "true", "未进行操作", null);
            }else {
                return new Result<>("400", "false", "修改失败", null);
            }

        }catch (Exception e){
            e.printStackTrace();
            throw new SsmException("异常");
        }


    }
    @PostMapping("/queryqx")
    public List<qxVO> queryqx(@RequestBody rolemenuDTO ro) {
        System.err.println(this.roleservice.queryqx(ro));
        return this.roleservice.queryqx(ro);
    }

    @PostMapping("/queryzqx")
    public List<qxVO> queryzqx(@RequestBody rolemenuDTO ro) {
        System.err.println(this.roleservice.queryzqx(ro));
        return this.roleservice.queryzqx(ro);
    }

    @PostMapping("/adddelqx")
    public Result adddelqx(@RequestBody rolemenuDTO ro) {
        System.err.println(ro);
        try{
            int i = 0;
            int a=0;
            if(!ro.getAdd().isEmpty()){
                i=this.roleservice.insertRoleauth(ro);
            }
            if(!ro.getDel().isEmpty()){
                a=this.roleservice.deleteRoleauth(ro);
            }
            if(i+a!=0){
                return new Result<>("200", "true", "修改成功", null);
            }else if(ro.getAdd().isEmpty()&&ro.getDel().isEmpty()){
                return new Result<>("200", "true", "未进行操作", null);
            }else {
                return new Result<>("400", "false", "修改失败", null);
            }

        }catch (Exception e){
            e.printStackTrace();
            throw new SsmException("异常");
        }


    }

}


